import axios from "axios"
import {$msg} from "@/utils/index"
import {getToken} from "@/utils/auth"
import router from "@/router"
import {clearAllStorage} from "@/utils/auth"

const service = axios.create({
    baseURL: '/api-dev',
    timeout: 5000
})
service.interceptors.request.use(
        (config) => {
            const token = getToken()
            if(!config.notToken){
                if(token){
                    config.headers["Authorization"] = "Bearer " + token
                }else{
                    $msg({
                        message: "请先登录",
                        type: "error"
                    })
                    clearAllStorage()
                    router.push('/login').then(r => {})
                }
            }
            return config
        },
        (error) => {
            return Promise.reject(error)
        }
)

service.interceptors.response.use(
        (response) => {
            if (response.status === 200) {
                return response
            } else {
                return Promise.reject(response)
            }
        },
        (error) => {
            return Promise.reject(error)
        }
);

export default service;
